// pages/my/signManage/fileManage/fileDetail.js
var util = require('../../../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    file_id: '', // 合同编号
    file: '', // 合同信息对象
    signManState: true,
    signManStateText: '展开',
    archiveHidden: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        var data = res.data;
        that.setData({
          userInfo: data,
          file_id: options.id
        });
        that.dataLoad();
      },
      fail: function() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  /**
   * 初始化数据
   */
  dataLoad: function() {
    wx.showLoading({
      title: '加载中···',
    });
    var that = this;
    util.getReq('/app/sign/getFileDetail', {
      file_id: that.data.file_id
    }, function(data) {
      if (data.code == 0) {
        var list = data.params.list;
        var data = data.data;
        that.setData({
          file: data,
          create_time: util.format(data.create_time, 'yyyy-MM-dd HH:mm:ss'),
          list: list
        });
      } else {
        util.isError(data.desc);
      }
      wx.hideLoading();
    });
  },

  /**
   * 合同预览
   */
  openFile: function(e) {
    var src = e.target.dataset.src;
    wx.navigateTo({
      url: '/pages/my/electronSign/previewPdf?pdfUrl=' + encodeURIComponent(src)
    })
  },

  /**
   * 查看签署方
   * 
   */
  seeSignMan: function() {
    var that = this;
    var signManState = that.data.signManState;
    if (signManState) {
      that.setData({
        signManState: false,
        signManStateText: '隐藏'
      });
    } else {
      that.setData({
        signManState: true,
        signManStateText: '展开'
      });
    }
  },

  /**
   * 归档确认框
   */
  archiveOperate: function() {
    this.setData({
      archiveHidden: false
    });
  },

  /**
   * 确认归档
   */
  onConfirm: function() {
    var that = this;
    that.setData({
      archiveHidden: true
    });
    wx.showLoading({
      title: '归档中···',
    });
    util.getReq('/app/sign/toArchive', {
      'file.file_id': that.data.file_id
    }, function(data) {
      if (data.code == 0) {
        wx.showToast({
          title: '归档成功',
        });
        that.dataLoad();
      } else {
        util.isError(data.desc);
      }
      wx.hideLoading();
    });
  },

  onCancel: function() {
    this.setData({
      archiveHidden: true
    });
  },

  startSign: function() {
    wx.navigateTo({
      url: '/pages/my/electronSign/fileManage/startSign?id=' + this.data.file_id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.dataLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})